#!/usr/bin/bash
#备份参数设置
#mongo容器名称
mongo_container_name=*********
#mongodb的用户名和密码及数据库名称
mongodb_username=******
mongodb_password=******
db_name=******
#设置需要备份的collection
collection_names=(****** ###### %%%%%%%)
#设置OSS用户名和密码
MINIO_ROOT_USER=******
MINIO_ROOT_PASSWORD=******
#设置bucket名称
bucket_name=******
#设置备份文件相关
backup_folder=~/backup/data
db_backup_folder_name=db
oss_backup_folder_name=oss
db_backup_folder=$backup_folder/$db_backup_folder_name
oss_backup_folder=$backup_folder/$oss_backup_folder_name
backup_name=backup$(date +%Y%m%d).tar.gz
#建立备份文件夹
mkdir -p $db_backup_folder
mkdir -p $oss_backup_folder

echo "Start backup database"
echo "开始备份数据库"
i=1
c_num=${#collection_names[*]}
while [[ $i -le $c_num ]]; do
	c_name=${collection_names[i-1]}
	docker exec -it $mongo_container_name /opt/bitnami/mongodb/bin/mongoexport -u $mongodb_username -p $mongodb_password -d $db_name -c $c_name -o /tmp/$c_name.json
	docker cp $mongo_container_name:/tmp/$c_name.json $db_backup_folder
	echo $c_name
	let i++
done
echo "Backup database successed!"
echo "数据库备份成功！"

echo "Start backup oss files"
echo "开始备份OSS文件"

#默认oss服务在本地且端口为9020，如有不同请修改
mc alias set ossbackup http://localhost:9020 $MINIO_ROOT_USER $MINIO_ROOT_PASSWORD
mc mirror ossbackup/$bucket_name/ $oss_backup_folder

echo "Start compress backup files......"
echo "开始压缩备份文件......"
cd ~/backup
tar -czvPf $backup_name data
echo "Database compressed file $backup_name has been created......"
echo "数据库压缩文件$db_backup_name已经创建完成......"